package com.tm.mholmu.db;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBServiceIpml {
	private static DBServiceIpml instance;
	private SQLiteDatabase db = null;
	private DBPoliceHelper dbOpenHelper;
 
	public synchronized static DBServiceIpml getInstatnce(Context ctx) {
		if (instance == null) {
			instance = new DBServiceIpml(ctx);
		}
		return instance;
	}

	private DBServiceIpml(Context context) {
		dbOpenHelper = new DBPoliceHelper(context);
	}

	public synchronized void closeDbService() {
		if (db != null) {
			db.close();
		}
	}

	public synchronized void deleteDoneFile() {
		db = dbOpenHelper.getWritableDatabase();

	}

	public List<Outfit> getOutfit(int i,String s) {
		List<Outfit> lo= new ArrayList<Outfit>();
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		try {
			
			// A and B or C or D or E 
			cursor = db.query(DBPoliceHelper.Tables.Dase_Data, null,
					DBPoliceHelper.BaseData.distance+"='"+s+"' and ("+
					DBPoliceHelper.BaseData.N_1 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_6 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_2 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_3 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_4 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_5 + " = "+i +")",
					null, null, null, null);
			
			System.err.println(DBPoliceHelper.BaseData.distance+"='"+s+"' and ("+
					DBPoliceHelper.BaseData.N_1 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_6 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_2 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_3 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_4 + " = "+i +" or "+
							DBPoliceHelper.BaseData.N_5 + " = "+i +")");
			CursorToOutfit(cursor, lo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return lo;
	}
	
	public  List<Outfit> CursorToOutfit(Cursor cursor,List<Outfit> lo) {
		while (cursor.moveToNext()) {
			Outfit o=new Outfit();
			o.id=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.id));
			o.distance=cursor.getString(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.distance));
			o.hole=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.hole));
			o.name=cursor.getString(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.name));
			o.part=cursor.getString(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.part));
			o.skill=cursor.getString(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.skill));
			
			o.N_1=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_1));
			o.N_2=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_2));
			o.N_3=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_3));
			o.N_4=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_4));
			o.N_5=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_5));
			o.N_6=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.N_6));
			
			o.Z_1=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_1));
			o.Z_2=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_2));
			o.Z_3=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_3));
			o.Z_4=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_4));
			o.Z_5=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_5));
			o.Z_6=cursor.getInt(cursor
					.getColumnIndex(DBPoliceHelper.BaseData.Z_6));
			lo.add(o);
		}
		cursor.close();
		return lo;
	}
	
	public void addCheckGoodsData(List<Outfit> list) {
		for (int i = 0; i < list.size(); i++) {
			// 添加核查数据
			db = dbOpenHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(DBPoliceHelper.BaseData.name, list.get(i).name);//
			values.put(DBPoliceHelper.BaseData.distance, list.get(i).distance);//
			values.put(DBPoliceHelper.BaseData.hole, list.get(i).hole);//
			values.put(DBPoliceHelper.BaseData.part, list.get(i).part);//
			values.put(DBPoliceHelper.BaseData.skill, list.get(i).skill);

			values.put(DBPoliceHelper.BaseData.N_1, list.get(i).N_1);//
			values.put(DBPoliceHelper.BaseData.N_2, list.get(i).N_2);//
			values.put(DBPoliceHelper.BaseData.N_3, list.get(i).N_3);//
			values.put(DBPoliceHelper.BaseData.N_4, list.get(i).N_4);//
			values.put(DBPoliceHelper.BaseData.N_5, list.get(i).N_5);//
			values.put(DBPoliceHelper.BaseData.N_6, list.get(i).N_6);//

			values.put(DBPoliceHelper.BaseData.Z_1, list.get(i).Z_1);//
			values.put(DBPoliceHelper.BaseData.Z_2, list.get(i).Z_2);//
			values.put(DBPoliceHelper.BaseData.Z_3, list.get(i).Z_3);//
			values.put(DBPoliceHelper.BaseData.Z_4, list.get(i).Z_4);//
			values.put(DBPoliceHelper.BaseData.Z_5, list.get(i).Z_5);//
			values.put(DBPoliceHelper.BaseData.Z_6, list.get(i).Z_6);//
			db.insert(DBPoliceHelper.Tables.Dase_Data, null, values);

		}
		// Toast.makeText(EviApplication.getInstance().getApplicationContext(),
		// ""+list.size(), 0).show();

	}

	public void AddAllSkill(List<Skill> s) {
		for (int i = 0; i < s.size(); i++) {
			// 添加核查数据
			db = dbOpenHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(DBPoliceHelper.SkillData.Id2, s.get(i).id2);//
			values.put(DBPoliceHelper.SkillData.Name, s.get(i).name);//
			values.put(DBPoliceHelper.SkillData.Point, s.get(i).point);//

			values.put(DBPoliceHelper.SkillData.Comment, s.get(i).comment);//
			db.insert(DBPoliceHelper.Tables.Skill_Data, null, values);
		}
	}

	
	
	public String getSkill_Name(int i){
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		String s="";
		if (i<0) {
			return "错误";
		}
		try {
			cursor = db.query(DBPoliceHelper.Tables.Skill_Data, null,
					DBPoliceHelper.SkillData.Id2 + " = "+i ,
					null, null, null, null);
		//	list = cursorToLoadInfo(cursor);
			while (cursor.moveToNext()) {
				s=cursor.getString(cursor
						.getColumnIndex(DBPoliceHelper.SkillData.Name));
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return s;
	}
	
	public int getSkill_Int(String skill_name) {
		int i = -999;
		if (skill_name.equals("")) {
			return 0;
		}
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		try {

			cursor = db.query(DBPoliceHelper.Tables.Skill_Data, null,
					DBPoliceHelper.SkillData.Name + " = '"+skill_name+"'" ,
					null, null, null, null);
		//	list = cursorToLoadInfo(cursor);
			while (cursor.moveToNext()) {
				i=cursor.getInt(cursor
						.getColumnIndex(DBPoliceHelper.SkillData.Id2));
			}
			
			
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}

		return i;
	}

	public void deleteCheckGoodsData() {/*
										// 删除全部核查数据
										db = dbOpenHelper.getWritableDatabase();
										db.delete(DBPoliceHelper.Tables.CHECK_GOODS_DATA, null, null);//删除表中所有的数据
										db = dbOpenHelper.getWritableDatabase();
										db.execSQL("DROP TABLE "+DBPoliceHelper.Tables.CHECK_GOODS_DATA);//删除这个表
										
										db.execSQL("CREATE TABLE " + Tables.CHECK_GOODS_DATA + " ("
										+ CheckGoodsData.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
										+ CheckGoodsData.Rfid + " TEXT NOT NULL, "
										+ CheckGoodsData.Information + " TEXT NOT NULL, "
										+ CheckGoodsData.State + "  INTEGER NOT NULL"
										+ ");");//重新创建表,这样索引才会初始化
										*/
	}

	/*public List<CheckRecord> GetCheckGoodsAfter(int start,int end) {
		// 得到指定区间的数据
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		List<CheckRecord> list = null;

		try {
			
			cursor = db.query(DBPoliceHelper.Tables.CHECK_GOODS_DATA, null,
					DBPoliceHelper.CheckGoodsData.ID + " > " + start + " and "
							+ DBPoliceHelper.CheckGoodsData.ID + " <= " + end,
					null, null, null, null);
			list = cursorToLoadInfo(cursor);
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}
	*/
	/*	public synchronized List<CheckRecord> cursorToLoadInfo(Cursor cur) {//把得到的数据结果转换为列表
			List<CheckRecord> item = null;
			try {
				if (cur != null) {
					item = new ArrayList<CheckRecord>();
					while (cur.moveToNext()) {
						CheckRecord li = new CheckRecord();
						li.setId(cur.getInt(cur
								.getColumnIndex(DBPoliceHelper.CheckGoodsData.ID)));
						li.setRfid(cur.getString(cur
								.getColumnIndex(DBPoliceHelper.CheckGoodsData.Rfid)));
						li.setInformation(cur.getString(cur
								.getColumnIndex(DBPoliceHelper.CheckGoodsData.Information)));
						li.setState(cur.getInt(cur
								.getColumnIndex(DBPoliceHelper.CheckGoodsData.State)));
						item.add(li);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
			}
			return item;
		
			return null;}
	*/
	/*public List<CheckRecord> GetNoCheckGoods(int start,int end ){
		// 得到指定区间没有核查的到的物品数据
	
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		List<CheckRecord> list = null;

		try {
			cursor = db.query(DBPoliceHelper.Tables.CHECK_GOODS_DATA, null,
					DBPoliceHelper.CheckGoodsData.ID + " > " + start + " and "
							+ DBPoliceHelper.CheckGoodsData.ID + " <= " + end+" and "+DBPoliceHelper.CheckGoodsData.State+"=0",
					null, null, null, null);
			list = cursorToLoadInfo(cursor);
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	

	
		return null;}*/

	/*	public void addCheckGoodsData(List<CheckRecord> list) {
			for (int i = 0; i < list.size(); i++) {
				// 添加核查数据
				db = dbOpenHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put(DBPoliceHelper.CheckGoodsData.Rfid, list.get(i)
						.getRfid());// rfid号
				values.put(DBPoliceHelper.CheckGoodsData.Information, list.get(i)
						.getInformation());// 物品信息
				values.put(DBPoliceHelper.CheckGoodsData.State, list.get(i)
						.getState());// 盘点状态

				db.insert(DBPoliceHelper.Tables.CHECK_GOODS_DATA, null, values);
			
			}
		//	Toast.makeText(EviApplication.getInstance().getApplicationContext(), ""+list.size(), 0).show();

		}*/

	/*
	public void SetCheckGoodsExist(String rfid) {
		// 设置某条数据为已核查
	db = dbOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(DBPoliceHelper.CheckGoodsData.State, 1);
	
		db.update(DBPoliceHelper.Tables.CHECK_GOODS_DATA, values, DBPoliceHelper.CheckGoodsData.Rfid + "= \""+rfid+"\"", null);
	}*/

	/*
	public int getDBchecktotal() {
		// TODO 自动生成的方法存根
		int count=0;
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		cursor =db.query(DBPoliceHelper.Tables.CHECK_GOODS_DATA, null,null,
				null, null, null, null);
		count=cursor.getCount();
		cursor.close();
		return	count;
	
	}*/

}
